import logging

from eshop.lib.base import *
#from model import *

DATA_TYPE_OPTIONS = (("String", 0), ("Integer", 1), ("Float", 2), ("Date", 3), ("Boolean", 4))

log = logging.getLogger(__name__)

class PropertyController(BaseController):

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        c.data_types = DATA_TYPE_OPTIONS
        return BaseController.__call__(self, environ, start_response)     
   
    def add(self):
        return render('/catalog/property_add.mako')
    
    def delete(self, id):
        model.ProductProperty.q.get(id).delete()
        redirect_to(action='index', id=None)
    
    def edit (self, id):
        c.property = model.ProductProperty.q.get(id)
        return render('/catalog/property_edit.mako')

    def index(self):
        c.properties = model.ProductProperty.q.all()
#        c.categories = model.ProductProperty.q.filter_by(parent_property_id=None)
        return render('/catalog/property_list.mako')
    
    def view (self, id=None):
        c.property = model.ProductProperty.q.get(id)
        return render('/catalog/property_view.mako')
    
    def save_edit(self, id):
        property = model.ProductProperty.q.get(id)
        property.description = request.params.get('description')
        property.data_type = request.params.get('data_type')
        redirect_to(action='index')
        
    def save_add(self):
        property = model.ProductProperty(\
            code=request.params.get('code'),\
            description=request.params.get('description'))
        property.data_type = request.params.get('data_type')
        redirect_to(action='index')
    
